package com.kwedding.service.app;

import com.kwedding.bean.TB_Customer;
import com.kwedding.dao.TB_CustomerMapper;
import com.kwedding.dto.app.RequestAddCustomer;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CustomerService extends LoggerParent {

	@Autowired
	private TB_CustomerMapper tb_CustomerMapper;

	/**
	 * 添加客户纪录
	 * 
	 * @param addCustomer
	 */
	@Transactional
	public void insertCustomer(RequestAddCustomer addCustomer) {
		CheckedUtil.isNull(addCustomer.getLinkman1(), Constants.REQUEST_ERROR_1, "联系人1不能为空");
		CheckedUtil.isNull(addCustomer.getLinkman2(), Constants.REQUEST_ERROR_2, "联系人2不能为空");
		CheckedUtil.isNull(addCustomer.getPhone1(), Constants.REQUEST_ERROR_3, "联系电话1不能为空");
		CheckedUtil.isNull(addCustomer.getPhone2(), Constants.REQUEST_ERROR_4, "联系电话2不能为空");
		CheckedUtil.isNull(addCustomer.getIsSuccess(), Constants.REQUEST_ERROR_5, "是否谈单成功不能为空");
		CheckedUtil.isNull(addCustomer.getBrandId(), Constants.REQUEST_ERROR_7, "所选品牌不能为空");
		CheckedUtil.isNull(addCustomer.getRefId(), Constants.REQUEST_ERROR_8, "销售员不能为空");
		TB_Customer tb_Customer = CopyerObject.copyerObject(addCustomer, TB_Customer.class);
		tb_Customer.setCustomerId(IDGenerate.generate());
		tb_Customer.setStatus(false);
		tb_Customer.setCreateTime(new Date());
		tb_Customer.setUpdateTime(new Date());
		if (tb_CustomerMapper.insert(tb_Customer) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_9, "添加顾客信息失败");
		}
	}

}
